import type { RootThunkAction } from "@/types/store"
import type { LoginForm, LoginResponse } from "@/types/data"
import { http } from "@/utils/http"
import { setToken, clearToken } from "@/utils/auth"
export const login = (loginParams: LoginForm): RootThunkAction => {
  return async (dispatch) => {

    const res = (await http.post(
      "/authorizations",
      loginParams
    )) as LoginResponse
    setToken(res.data)
    dispatch({ type: "login/token", payload: res.data }) 
  }
}


export const sendCode = (mobile: string): RootThunkAction => {
  return async (dispatch) => {
    return http.get(`/sms/codes/${mobile}`) 
  }
}


export const logout = (): RootThunkAction => {
  return (dispatch) => {
    dispatch({ type: "login/logout" }) 
    clearToken() 
  }
}
